package org.thotheolh.sweatkeeper.ui;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JToolBar;
import javax.swing.table.DefaultTableModel;

import org.thotheolh.sweatkeeper.helper.Equipment;
import org.thotheolh.sweatkeeper.util.Resource;

public class EquipmentPanel extends JPanel {
	
	private JToolBar toolBar;
	private JButton newEquipmentBtn;
	private JPanel contentPanel;
	private JTable table;
	private JButton editEquipmentBtn;
	private JButton delBtn;
	private MyDefaultTableModel model;
	/**
	 * Create the panel.
	 */
	public EquipmentPanel() {

		setLayout(new BorderLayout(0, 0));
		toolBar = new JToolBar();
		this.add(toolBar, BorderLayout.NORTH);
		
		newEquipmentBtn = new JButton("New Equipment");
		newEquipmentBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				NewEquipmentDialog dialog = new NewEquipmentDialog();
				dialog.setLocationRelativeTo(null);
				dialog.setVisible(true);
		}});
		toolBar.add(newEquipmentBtn);
		
		editEquipmentBtn = new JButton("Edit Equipment");
		editEquipmentBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				editEquipment();
		}});
		toolBar.add(editEquipmentBtn);
		
		delBtn = new JButton("Delete Equipment");
		delBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				delEquipment();
		}});
		toolBar.add(delBtn);
		
		contentPanel = new JPanel();
		contentPanel.setLayout(new BorderLayout());
		
		add(contentPanel, BorderLayout.CENTER);
		
		JScrollPane scrollPane = new JScrollPane();
		add(scrollPane, BorderLayout.CENTER);
		
		table = new JTable();
		model = new MyDefaultTableModel();
		table.setModel(model);
		model.addColumn("EQUIPMENT ID");
		model.addColumn("EQUIPMENT NAME");
		model.addColumn("COMMENT");
		table.getColumnModel().getColumn(0).setPreferredWidth(10);
		table.getColumnModel().getColumn(1).setPreferredWidth(147);
		table.getColumnModel().getColumn(2).setPreferredWidth(147);
		scrollPane.setViewportView(table);
		
		initData();
		Resource.getInstance().registerRefreshable(this);
	}
	
	public void initData(){
		model.getDataVector().removeAllElements();
		ArrayList ls = Resource.getInstance().getSql().getAllEquipments();
		if (ls != null && (!ls.isEmpty())){
			for (int i = 0; i < ls.size(); i++){
				Equipment tmp = (Equipment)ls.get(i);
				String[] row = new String[3];
				row[0] = ""+tmp.getEqId();
				row[1] = ""+tmp.getEqName();
				row[2] = ""+tmp.getEqComments();
				model.addRow(row);
			}
		}
	}
	
	public void editEquipment(){
		int[] rows = table.getSelectedRows();
		int col = 0;
		if (rows.length != 1){
			JOptionPane.showMessageDialog(null, "Please select only 1 row to edit !", "Too Many Rows Selected", JOptionPane.WARNING_MESSAGE);
		} else {
			for (int i = 0; i < model.getColumnCount(); i++){
				if (model.getColumnName(i).equals("EQUIPMENT ID")){
					col = i;
				}
			}
			for (int i = 0; i < rows.length; i++){
				new EditEquipmentDialog(Integer.parseInt(model.getValueAt(rows[i], col).toString())).setVisible(true);
			}
		}
	}
	
	public void delEquipment(){
		int[] rows = table.getSelectedRows();
		int col = 0;
		int delProb = 0;
		if (rows.length <= 0 || rows == null){
			JOptionPane.showMessageDialog(null, "Please select an equipment to delete !", "No Rows Selected", JOptionPane.WARNING_MESSAGE);
		} else {
			for (int i = 0; i < model.getColumnCount(); i++){
				if (model.getColumnName(i).equals("EQUIPMENT ID")){
					col = i;
				}
			}
			int num = JOptionPane.showConfirmDialog(null, "Do you really want to delete the " + rows.length +" selected equipment(s) ?", "Delete Equipment(s) ?", JOptionPane.YES_NO_OPTION);
			if (num == JOptionPane.YES_OPTION){
				for (int i = 0; i < rows.length; i++){
					int value = Integer.parseInt(model.getValueAt(rows[i], col).toString());
					boolean deleted = Resource.getInstance().getSql().rmEquipment(value);
					if (!deleted){
						delProb = delProb++;
					}
				}
				if(delProb != 0){
					JOptionPane.showMessageDialog(null, "There are " + delProb + "equipment that have failed to be deleted !", "Delete Equipment Failed", JOptionPane.ERROR_MESSAGE);
				}
				Resource.getInstance().callRefreshable();
			}			
		}
	}
	
	public class MyDefaultTableModel extends DefaultTableModel {

        public MyDefaultTableModel() {
            super();
        }

        @Override
        public boolean isCellEditable(int row, int col) {
            return false;
        }
    }

}
